package cn.gson.crm.controller;

import cn.gson.crm.model.pojos.*;
import cn.gson.crm.model.service.Cpservice;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/cp")
public class CpController {
    String tp;
    String uri = "E:/upload";
    @Autowired
    Cpservice cpservice;
    @RequestMapping("/cpp")
    @ResponseBody
    public Map<String,Object> cpp(Integer currentPage,Integer pageSize,String name,String fenlei){
        if(currentPage==null || currentPage==' '||pageSize==null||pageSize==' '){
            currentPage=1;
            pageSize=5;
        }
        Page<Object> obj = PageHelper.startPage(currentPage,pageSize);
        List<Pro> cpp = cpservice.cx(name,fenlei);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("row",cpp);
        return map;
    }

    //查询类别
    @RequestMapping("/lb")
    @ResponseBody
    public List<Category> lb(){
        return cpservice.lb();
    }

    //按类别查询规格
    @RequestMapping("/guige")
    @ResponseBody
    public Map<String,Object> gg(String bb){
        System.out.println("bbb:"+bb);
        List<Pro> list = cpservice.gg(bb);
        Map<String,Object> map = new HashMap<>();
        map.put("row",list);
        return map;
    }


    //新增

    @PostMapping("/xinzen")
    @ResponseBody
    //@RequestParam("cpxz")
    public String xz(@RequestParam("file") MultipartFile file,@RequestParam("cpxz") String cpxz){
        System.out.println("1234567890");
        System.out.println("file："+file);
        System.out.println("zz:"+cpxz);
        System.err.println("长度："+cpxz.length());
        JSONObject obj = (JSONObject) JSONObject.parse(cpxz);
        Cpxz cpxz1 = obj.getObject("cpxz",Cpxz.class);
        String fileName = file.getOriginalFilename();
        String ext = FilenameUtils.getExtension(fileName);
        //生成一个新的名字
        String newName = UUID.randomUUID().toString();
        String filePath = newName + "." + ext;

        try {
            File f = new File(uri,filePath);
            if(!f.exists()) {
                f.mkdirs();//如果目录不存在，创建
            }
            //文件转移
            file.transferTo(f);
            //将路径存进数据库

            tp="upload/" + filePath;
            cpservice.cpxz(cpxz1,tp);
            //回显路径
            return  "upload/" + filePath;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //根据规格名id查询规格名
    @RequestMapping("/guigeming")
    @ResponseBody
    public List<Specifications> guigeming(Integer guigeid,Integer guigezhiid){
        return cpservice.guigeming(guigeid,guigezhiid);
    }
    //根据规格值id查询规格值
//    @RequestMapping("/guigezhi")
//    @ResponseBody
//    public List<Specifications> guigezhi(Integer guigezhiid){
//        return cpservice.guigezhi(guigezhiid);
//    }

    @RequestMapping("/cggz")
    @ResponseBody
    public List<SpecifValue> ming(String cpming,String lbming){
        return cpservice.ming(cpming,lbming);
    }

    @RequestMapping("/cgmx")

    @ResponseBody
    public List<Purchasexq> cgmx(){
        return cpservice.cgmx();
    }


    @RequestMapping("/xqId")
    @ResponseBody
    public List<ProductXq> xqId(Integer xqId){
        return cpservice.xqId(xqId);
    }

    @RequestMapping("/xg")
    @ResponseBody
    public void sc(Integer cpId){
        cpservice.xg(cpId);
    }

}
